Skip to content

fix: parse tool calls in streaming reasoning branch#177

Closed
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Thump604:fix/streaming-reasoning-tool-coexistence-v2
Closed

fix: parse tool calls in streaming reasoning branch#177
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Thump604:fix/streaming-reasoning-tool-coexistence-v2

Conversation

@Thump604
Copy link
Copy Markdown
Collaborator

@Thump604 Thump604 commented Mar 18, 2026

Fix streaming when both reasoning (thinking) tags and tool calls appear in the same response.

Before: reasoning parser consumed <think> content but left artifacts that broke tool call parsing downstream.
After: reasoning extraction runs first, cleaned text passed to tool parser.

Files: server.py streaming handler

Test: Send a request with tools enabled and a model that uses <think> tags. Response should have both reasoning_content and tool_calls populated correctly.

When reasoning parsing is enabled, the streaming handler uses an if/else
structure where the reasoning parser branch and tool parser branch are
mutually exclusive. This means tool calls in streaming responses are
never parsed when reasoning is enabled — they come through as raw XML
text instead of structured tool_calls.

Fix: Add tool call accumulation and parsing inside the reasoning parser
branch, operating on delta_msg.content (the content portion after
reasoning extraction). Handles all three tool parser outcomes: suppress
(inside markup), emit structured tool_calls, or pass through content.

Also fixes finish_reason to return "tool_calls" when tools were detected
in the reasoning branch, matching the behavior of the standard path.
@Thump604
Copy link
Copy Markdown
Collaborator Author

Production evidence from M2 Ultra 128GB, Qwen3.5-122B-A10B, BatchedEngine with MTP routing and streaming:

Without this fix, tool calls in the streaming reasoning branch are silently dropped. The reasoning parser consumes the <tool_call> tags as part of the thinking output, and the tool call parser never sees them.

The fix adds tool call parsing to the streaming reasoning branch so both reasoning content and tool calls are extracted correctly.

Tested with OpenCode (multi-turn agent), Cline, and direct API calls with stream=true + tools enabled.

@Thump604
Copy link
Copy Markdown
Collaborator Author

Thump604 commented Apr 9, 2026

Status ping — this PR has been idle 8 days and was the one Thump604 PR skipped from the 2026-04-07 coordinated rollup. No known blockers on our side: branch is rebased against current main (6680b180), CI is green, and the fix closes #161 (reasoning content leaks into tool_calls on the streaming path). Flagging for visibility.

@Thump604
Copy link
Copy Markdown
Collaborator Author

I rechecked this in light of the new official Gemma 4 chat templates. My read is the same: that template update does not obsolete this streaming parser fix.

The template update changes how messages are rendered into the prompt. This PR is about streamed model output when reasoning blocks and tool calls appear together. Those are different layers of the stack.

I also revalidated the live Gemma lane after the template sync: tool calling, tool-result replay, and plain reasoning split are all still green. So I would keep this PR in the queue as a real fix candidate, but it probably wants to be restacked with the newer raw-output-preservation work rather than evaluated in isolation against an older server state.

@janhilgard
Copy link
Copy Markdown
Collaborator

Hey @Thump604 — this fix is now covered in main via PR #278, which backported the full reasoning parser + tool parser streaming pipeline from production. The reasoning extraction runs first and passes cleaned text to the tool parser, so tool calls inside <think> responses work correctly.

This PR has merge conflicts with current main since server.py changed significantly. Safe to close as superseded if you agree.

@Thump604
Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #278. The merged reasoning-parser/tool-parser streaming pipeline on current main covers this path, and this branch now conflicts with the updated server changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants